/**
 * 
 */
// ajax没有缓存
$.ajaxSetup( {
	cache : false
// close AJAX cache
		});
//提供java的ArrayList的功能
var ArrayList = function() {
	var args = ArrayList.arguments;
	var initialCapacity = 10;

	if (args != null && args.length > 0) {
		initialCapacity = args[0];
	}

	var elementData = new Array(initialCapacity);
	var elementCount = 0;

	this.size = function() {
		return elementCount;
	};

	this.add = function(element) {
		ensureCapacity(elementCount + 1);
		elementData[elementCount++] = element;
		return true;
	};

	this.addElementAt = function(index, element) {
		if (index > elementCount || index < 0) {
			alert("IndexOutOfBoundsException, Index: " + index + ", Size: "
					+ elementCount);
			return;
			//throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));
		}
		ensureCapacity(elementCount + 1);
		for ( var i = elementCount + 1; i > index; i--) {
			elementData[i] = elementData[i - 1];
		}
		elementData[index] = element;
		elementCount++;
	};

	this.setElementAt = function(index, element) {
		//alert("setElementAt");
		if (index > elementCount || index < 0) {
			alert("IndexOutOfBoundsException, Index: " + index + ", Size: "
					+ elementCount);
			return;
			//throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));
		}
		elementData[index] = element;
	};

	this.toString = function() {
		//alert("toString()");
		var str = "{";
		for ( var i = 0; i < elementCount; i++) {
			if (i > 0) {
				str += ",";
			}
			str += elementData[i];
		}
		str += "}";
		return str;
	};

	this.get = function(index) {
		//alert("elementAt");
		if (index >= elementCount) {
			alert("ArrayIndexOutOfBoundsException, " + index + " >= "
					+ elementCount);
			return;
			//throw ( new Error( -1,"ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount ) );
		}
		return elementData[index];
	};

	this.remove = function(index) {
		if (index >= elementCount) {
			alert("ArrayIndexOutOfBoundsException, " + index + " >= "
					+ elementCount);
			//return;
			throw (new Error(-1, "ArrayIndexOutOfBoundsException, " + index
					+ " >= " + elementCount));
		}
		var oldData = elementData[index];
		for ( var i = index; i < elementCount - 1; i++) {
			elementData[i] = elementData[i + 1];
		}
		elementData[elementCount - 1] = null;
		elementCount--;
		return oldData;
	};

	this.isEmpty = function() {
		return elementCount == 0;
	};

	this.indexOf = function(elem) {
		//alert("indexOf");
		for ( var i = 0; i < elementCount; i++) {
			if (elementData[i] == elem) {
				return i;
			}
		}
		return -1;
	};

	this.lastIndexOf = function(elem) {
		for ( var i = elementCount - 1; i >= 0; i--) {
			if (elementData[i] == elem) {
				return i;
			}
		}
		return -1;
	};

	this.contains = function(elem) {
		return this.indexOf(elem) >= 0;
	};

	function ensureCapacity(minCapacity) {
		var oldCapacity = elementData.length;
		if (minCapacity > oldCapacity) {
			var oldData = elementData;
			var newCapacity = parseInt((oldCapacity * 3) / 2 + 1);
			if (newCapacity < minCapacity) {
				newCapacity = minCapacity;
			}
			elementData = new Array(newCapacity);
			for ( var i = 0; i < oldCapacity; i++) {
				elementData[i] = oldData[i];
			}
		}
	}
};
//实现java的HashMap的一部分
var HashMap = function(){
	this.put = function(key,value){
		this[key] = value
	 };
	this.get = function(key){
		return this[key]
	};
	this.contains = function(key){
		return this.Get(key) == null?false:true
	};
	this.remove = function(key){
		delete this[key]
	};
}
//实现replaceAll功能
String.prototype.replaceAll = function(s1, s2) {
	return this.replace(new RegExp(s1, "gm"), s2);
}

function birthdaySplit(birthday) {
	if (IsNull(birthday)) {
		var a = new Array(3);
		a[0] = '';
		a[1] = '';
		a[2] = '';
		return a;
	}
	return birthday.split("-");
}
/*初始化年
 *  year:jquery 对象
 * */
function yearInit(year) {
	var date = new Date();
	var currentYear = date.getFullYear();
	var yearOption = "";
	yearOption += "<option value=''> 请选择</option>"
	for ( var i = currentYear; i > currentYear - 60; i--) {
		yearOption += "<option value=" + i + "> " + i + "</option>";
	}
	year.empty();
	$(yearOption).appendTo(year);
}
/*初始化月
 *  month:jquery 对象
 * */
function monthInit(month) {
	var monthOption = "";
	monthOption += "<option value=''> 请选择</option>"
	for ( var i = 1; i < 13; i++) {
		var j = "";
		if (i < 10) {
			j = "0" + i;
		} else {
			j = i;
		}
		monthOption += "<option value=" + j + "> " + i + "</option>";
	}
	month.empty();
	$(monthOption).appendTo(month);
}
/*初始化年月日
 *  birthday:jquery 对象，年月日的上一层
 * */
function yearMonthDay(birthday, strBirthday) {
	var year = birthday.find("#year");
	var month = birthday.find("#month");
	var day = birthday.find("#day");
	yearInit(year);
	monthInit(month);
	var str = "";
	str += "<option value=''>请选择</option>"
	for ( var i = 1; i < 32; i++) {
		var j = "";
		if (i < 10) {
			j = "0" + i;
		} else {
			j = i;
		}
		str += "<option value=" + j + "> " + i + "</option>";
	}
	$(str).appendTo(day);

	if (!IsNull(strBirthday)) {
		var a = birthdaySplit(strBirthday);
		year.val(a[0]);
		month.val(a[1]);
		day.val(a[2]);
	}

	month.change(function() {
		var yearstr = year.find("option:selected").val();
		var monthstr = month.find("option:selected").text();
		day.empty();
		var str = "<option value=''>请选择</option>";

		if (monthstr == 1 || monthstr == 3 || monthstr == 5 || monthstr == 7
				|| monthstr == 8 || monthstr == 10 || monthstr == 12) {
			for ( var i = 1; i < 32; i++) {
				var j = "";
				if (i < 10) {
					j = "0" + i;
				} else {
					j = i;
				}
				str += "<option value=" + j + "> " + i + "</option>";
			}
			$(str).appendTo(day);
		} else if (monthstr == 2) {
			day.empty();
			if (yearstr % 100 != 0 && yearstr % 4 == 0 || yearstr % 100 == 0
					&& yearstr % 400 == 0) {
				for ( var i = 1; i < 30; i++) {
					var j = "";
					if (i < 10) {
						j = "0" + i;
					} else {
						j = i;
					}
					str += "<option value=" + j + "> " + i + "</option>";
				}
			} else {
				for ( var i = 1; i < 29; i++) {
					var j = "";
					if (i < 10) {
						j = "0" + i;
					} else {
						j = i;
					}
					str += "<option value=" + j + "> " + i + "</option>";
				}
			}
			$(str).appendTo(day);
		} else {
			day.empty();
			for ( var i = 1; i < 31; i++) {
				var j = "";
				if (i < 10) {
					j = "0" + i;
				} else {
					j = i;
				}
				str += "<option value=" + j + "> " + i + "</option>";
			}
			$(str).appendTo(day);
		}
	});
	year.change(function() {
		var yearstr = year.find("option:selected").val();
		var monthstr = month.find("option:selected").text();
		var str = "";
		if (monthstr == 2) {
			day.empty();
			if (yearstr % 100 != 0 && yearstr % 4 == 0 || yearstr % 100 == 0
					&& yearstr % 400 == 0) {
				for ( var i = 1; i < 30; i++) {
					var j = "";
					if (i < 10) {
						j = "0" + i;
					} else {
						j = i;
					}
					str += "<option value=" + j + "> " + i + "</option>";
				}
			} else {
				for ( var i = 1; i < 29; i++) {
					var j = "";
					if (i < 10) {
						j = "0" + i;
					} else {
						j = i;
					}
					str += "<option value=" + j + "> " + i + "</option>";
				}
			}
			$(str).appendTo(day);
		}
	});
}

/**
 * 时间对象的格式化;
 */
Date.prototype.format = function(format) {
	/*
	 * eg:format="YYYY-MM-dd hh:mm:ss";
	 */
	var o = {
		"M+" : this.getMonth() + 1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
		"S" : this.getMilliseconds()
	// millisecond
	}

	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	}

	for ( var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
}
//判断对象是否为空
function IsNull(i) {
	return (i == null || i == 'null' || i == '' || i == 'undefined');
}

///////////////////////////////////////////////////////////////////////////////

//得到客户端页面的高度 
function getTotalHeight() {
	var height = document.body.scrollHeight;
	return height;
}

function getBodyHeight() {
	return $(document).height();
}

function getBodyWidth() {
	return $(document).width();
}

function getClientHeight() {
	return document.documentElement.clientHeight;
}

function getMiddleHeight(dialogHeight) {
	if (IsNull(dialogHeight)) {
		dialogHeight = 230;
	}
	var clientHeight = getClientHeight();
	return clientHeight / 2 - dialogHeight / 2;
}
function getMiddleWidth(dialogWidth) {
	if (IsNull(dialogWidth)) {
		dialogWidth = 316;
	}
	var clientWidth = getBodyWidth();
	return clientWidth / 2 - dialogWidth / 2;
}
//显示遮罩
function showMask() {
	if($("#mask").size() < 1){
		var maskHtml = '<div id="mask" class="Mask"></div>';
		$(document.body).append(maskHtml);
	}
	$("#mask").css("height", getTotalHeight() + "px");
	$("#mask").show();
}

//隐藏遮罩
function hideMask() {
	$("#mask").hide();
}
/**
 日期相加
 * strType：要加的日期是秒，分钟，小时，天数，周数等
 * num：加的数
 * dtTmp：要被加的日期，类型为Date
 */
function dateAdd(strType, num, dtTmp) {
	if (isNaN(dtTmp))
		dtTmp = new Date();
	switch (strType) {
	//秒
	case "s":
		dtTmp = new Date(Date.parse(dtTmp) + (1000 * parseInt(num)));
		break;
	//		分钟
	case "n":
		dtTmp = new Date(Date.parse(dtTmp) + (60000 * parseInt(num)));
		break;
	//		小时
	case "h":
		dtTmp = new Date(Date.parse(dtTmp) + (3600000 * parseInt(num)));
		break;
	//		天数
	case "d":
		dtTmp = new Date(Date.parse(dtTmp) + (86400000 * parseInt(num)));
		break;
	//		周数
	case "w":
		dtTmp = new Date(Date.parse(dtTmp) + ((86400000 * 7) * parseInt(num)));
		break;
	//		月数
	case "m":
		dtTmp = new Date(dtTmp.getFullYear(), (dtTmp.getMonth())
				+ parseInt(num), dtTmp.getDate(), dtTmp.getHours(), dtTmp
				.getMinutes(), dtTmp.getSeconds());
		break;
	//		年数
	case "y":
		dtTmp = new Date(dtTmp.getFullYear() + parseInt(num), dtTmp.getMonth(),
				dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp
						.getSeconds());
		break;
	}

	return dtTmp;
}
//得到星期数组
function getWeekDay(date) {
	if (IsNull(date))
		date = new Date();
	var x = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
	return (x[date.getDay()]);
}
/**
 将字符串转换成日期
 * 格式：yyyy-MM-dd hh:mm:ss
 */
function strToDate(str) {
	var strArray = str.split(" ");
	var ymd = strArray[0];
	var ymdArray = ymd.split("-");
	var parseDate = ymdArray[1] + "/" + ymdArray[2] + "/" + ymdArray[0];
	if (!IsNull(strArray[1])) {
		parseDate += " ";
		parseDate += strArray[1];
	}
	var val = Date.parse(parseDate);
	var newDate = new Date(val);
	return newDate;
}
//字符转换成html格式编码
function htmlEncode(str) {
	var s = "";
	if (str.length == 0)
		return "";
	s = str.replace(/&/g, "&gt;");
	s = s.replace(/</g, "&lt;");
	s = s.replace(/>/g, "&gt;");
	s = s.replace(/ /g, "&nbsp;");
	s = s.replace(/\'/g, "&#39;");
	s = s.replace(/\"/g, "&quot;");
	s = s.replace(/\n/g, "<br>");
	return s;
}
//字符转换成html格式解码
function htmlDecode(str) {
	var s = "";
	if (str.length == 0)
		return "";
	s = str.replace(/&gt;/g, "&");
	s = s.replace(/&lt;/g, "<");
	s = s.replace(/&gt;/g, ">");
	s = s.replace(/&nbsp;/g, " ");
	s = s.replace(/&#39;/g, "\'");
	s = s.replace(/&quot;/g, "\"");
	s = s.replace(/<br>/g, "\n");
	return s;
}

var evn_Path = getPath();
function getPath() {
	//var envPath = document.location.pathname;
	var protoStr = document.location.protocol;
	var hostStr = document.location.host;
	var portStr = document.location.port;
	//var envPathArray = envPath.split('/');
	return protoStr + "//" + hostStr + "/";
}

/**
 * 地址的操作
 * author Bruce Zhang
 * @param provinceId		省/直辖市的ID
 * @param cityId		城市/直辖市的区
 * @param countyId		县/直辖市的街道
 * @param townId		镇
 */
function initProvince(provinceId, cityId, countyId, townId) {
	$.get(evn_Path + "getProvince.ajax", function(data) {
		$("#" + provinceId).html("<option value='0'>请选择</option>");
		$("#" + cityId).html("<option value='0'>请选择</option>");
		$("#" + countyId).html("<option value='0'>请选择</option>");
		$("#" + townId).html("<option value='0'>请选择</option>");
		$.each(data, function(i) {
			$("#" + provinceId).append(
					"<option value='" + data[i].province + "'>" + data[i].value
							+ "</option>");
		});
	}, 'json');
	$("#" + provinceId).change(
			function() {
				$.get(evn_Path + "getCity.ajax?provinceId="
						+ $("#" + provinceId).val(), function(data) {
					$("#" + cityId).html("<option value='0'>请选择</option>");
					$("#" + countyId).html("<option value='0'>请选择</option>");
					$("#" + townId).html("<option value='0'>请选择</option>");
					$.each(data, function(i) {
						$("#" + cityId).append(
								"<option value='" + data[i].city + "'>"
										+ data[i].value + "</option>");
					});
				}, 'json');
			});

	$("#" + cityId).change(
			function() {
				$.get(evn_Path + "getCounty.ajax?cityId="
						+ $("#" + cityId).val(), function(data) {
					$("#" + countyId).html("<option value='0'>请选择</option>");
					$("#" + townId).html("<option value='0'>请选择</option>");
					$.each(data, function(i) {
						$("#" + countyId).append(
								"<option value='" + data[i].county + "'>"
										+ data[i].value + "</option>");
					});
				}, 'json');
			});

	$("#" + countyId).change(
			function() {
				$.get(evn_Path + "getTown.ajax?countyId="
						+ $("#" + countyId).val(), function(data) {
					$("#" + townId).html("<option value='0'>请选择</option>");
					$.each(data, function(i) {
						$("#" + townId).append(
								"<option value='" + data[i].town + "'>"
										+ data[i].value + "</option>");
					});
				}, 'json');
			});
}

function provinceChange(provinceId, cityId, countyId, townId, provinceValue,
		cityValue, countyValue, townValue) {
	$.get(evn_Path + "getProvince.ajax", function(data) {
		$("#" + provinceId).html("<option value='0'>请选择</option>");
		$("#" + cityId).html("<option value='0'>请选择</option>");
		$("#" + countyId).html("<option value='0'>请选择</option>");
		$("#" + townId).html("<option value='0'>请选择</option>");
		$.each(data, function(i) {
			if (data[i].province == provinceValue) {
				$("#" + provinceId).append(
						"<option selected value='" + data[i].province + "'>"
								+ data[i].value + "</option>");
			} else {
				$("#" + provinceId).append(
						"<option value='" + data[i].province + "'>"
								+ data[i].value + "</option>");
			}

		});
		//////////////////
			$.get(evn_Path + "getCity.ajax?provinceId=" + provinceValue,
					function(data) {
						$("#" + cityId).html("<option value='0'>请选择</option>");
						$("#" + countyId)
								.html("<option value='0'>请选择</option>");
						$("#" + townId).html("<option value='0'>请选择</option>");
						$.each(data, function(i) {
							if (data[i].city == cityValue) {
								$("#" + cityId).append(
										"<option selected value='"
												+ data[i].city + "'>"
												+ data[i].value + "</option>");
							} else {
								$("#" + cityId).append(
										"<option value='" + data[i].city + "'>"
												+ data[i].value + "</option>");
							}

						});
						//////////////
					$.get(evn_Path + "getCounty.ajax?cityId=" + cityValue,
							function(data) {
								$("#" + countyId).html(
										"<option value='0'>请选择</option>");
								$("#" + townId).html(
										"<option value='0'>请选择</option>");
								$.each(data, function(i) {
									if (data[i].county == countyValue) {
										$("#" + countyId).append(
												"<option selected value='"
														+ data[i].county + "'>"
														+ data[i].value
														+ "</option>");
									} else {
										$("#" + countyId).append(
												"<option value='"
														+ data[i].county + "'>"
														+ data[i].value
														+ "</option>");
									}

								});
								///////////////
							$.get(evn_Path + "getTown.ajax?countyId="
									+ countyValue, function(data) {
								$("#" + townId).html(
										"<option value='0'>请选择</option>");
								$.each(data, function(i) {
									if (data[i].town == townValue) {
										$("#" + townId).append(
												"<option selected value='"
														+ data[i].town + "'>"
														+ data[i].value
														+ "</option>");
									} else {
										$("#" + townId).append(
												"<option value='"
														+ data[i].town + "'>"
														+ data[i].value
														+ "</option>");
									}

								});
							}, 'json');
							//////////////
						}, 'json');
					///////////
				}, 'json');
			/////////////////
		}, 'json');

	$("#" + provinceId).change(
			function() {
				$.get(evn_Path + "getCity.ajax?provinceId="
						+ $("#" + provinceId).val(), function(data) {
					$("#" + cityId).html("<option value='0'>请选择</option>");
					$("#" + countyId).html("<option value='0'>请选择</option>");
					$("#" + townId).html("<option value='0'>请选择</option>");
					$.each(data, function(i) {
						$("#" + cityId).append(
								"<option value='" + data[i].city + "'>"
										+ data[i].value + "</option>");
					});
				}, 'json');
			});

	$("#" + cityId).change(
			function() {
				$.get(evn_Path + "getCounty.ajax?cityId="
						+ $("#" + cityId).val(), function(data) {
					$("#" + countyId).html("<option value='0'>请选择</option>");
					$("#" + townId).html("<option value='0'>请选择</option>");
					$.each(data, function(i) {
						$("#" + countyId).append(
								"<option value='" + data[i].county + "'>"
										+ data[i].value + "</option>");
					});
				}, 'json');
			});

	$("#" + countyId).change(
			function() {
				$.get(evn_Path + "getTown.ajax?countyId="
						+ $("#" + countyId).val(), function(data) {
					$("#" + townId).html("<option value='0'>请选择</option>");
					$.each(data, function(i) {
						$("#" + townId).append(
								"<option value='" + data[i].town + "'>"
										+ data[i].value + "</option>");
					});
				}, 'json');
			});
}

function setDialogPosition(id) {
	$('#' + id).css("top", getMiddleHeight() + "px");
	$('#' + id).css("left", getMiddleWidth() + "px");
}


	//让层弹出在中间
	function showDivMiddle(divId){
		var load = $("#"+divId);
		var width = load.width();
		var height = load.height();
		var left = (screen.width - width) / 2
				+ document.documentElement.scrollLeft;
		var top = (screen.height - height) / 2
				+ getScrollTop() - 200;
		$(load).css({
			"left" : left,
			"top" : top
		});
		$(load).show();
		$(load).bgiframe();
		showMask();
	}
	//让弹出的层在消失
	function hideDivMiddle(divId){
		$("#"+divId).hide();
		hideMask();
	}
	
	function insertUpload(msg) {
		$("#preImg").html("图片预览：<img width='200' height='150' src='" + msg[0].url + "'/>");
		$("#upload_id").val(msg[0].uploadId);
	}
